package com.powernode.lambda.stream;

import com.powernode.lambda.Student;

import java.util.Comparator;
import java.util.stream.Stream;

/**
 * 测试stream相关Api distinct、sorted、compare和二次排序
 */
public class Stream07 {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 2, 3);

        //去重
        stream.distinct().forEach(System.out::println);

        Stream<Student> studentStream = Stream.of(
                new Student("张无忌", 26, "男"),
                new Student("赵敏", 22, "女"),
                new Student("周芷若", 21, "女"),
                new Student("女波斯圣使", 30, "女"),
                new Student("灭绝师太", 30, "女"),
                new Student("张三丰", 100, "男")
        );
        //排序比较
        studentStream
//                .sorted((x,y)-> x.getAge()>y.getAge()?1:x.getAge()==y.getAge()?0:-1)
//                .sorted((x,y)-> Integer.compare(x.getAge(), y.getAge()))
//                .sorted(Comparator.comparingInt(student -> student.getAge())) //按照年龄升序
//                .sorted(Comparator.comparingInt(Student::getAge).reversed()) //按照年龄降序
                .sorted(Comparator.comparingInt(Student::getAge).reversed()
                        .thenComparing(student -> student.getName().length())) //按照年龄降序后在根据名字长度升序排序
                .forEach(System.out::println);
    }

}
